package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcUserType;
import com.csun.cmny.provider.model.dto.OmcUserTypeDto;
import com.csun.cmny.provider.model.dto.OmcUserTypeQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcUserTypeService;
import com.csun.cmny.provider.utils.ValidateUtil;
import com.csun.cmny.provider.validator.OmcUserTypeValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chenzheng
 * @description:人员类别控制器
 * @date 2019/8/10 11:52
 */
@RestController
@RequestMapping(value = "/userType", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcUserTypeController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcUserTypeService omcUserTypeService;

    /**
     * 添加基础人员类别
     * @param: [groupId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/addBasePeopleTypes", method = RequestMethod.POST)
    public Wrapper addBasePeopleTypes(@RequestParam(name = "groupId") Long groupId) {
        int ret = omcUserTypeService.addBasePeopleTypes(groupId,getLoginAuthDto());
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 添加人员类别
     *
     * @param omcUserTypeDto
     * @param br
     * @return
     */
    @ValidateAnnotation
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@Validated(value = {OmcUserTypeValidator.class}) @RequestBody OmcUserTypeDto omcUserTypeDto, BindingResult br) {
        if (null == omcUserTypeDto.getGroupId() || omcUserTypeDto.getGroupId() <= 0) {
            //没传机构ID,存储登录用户的机构ID
            omcUserTypeDto.setGroupId(getLoginAuthDto().getGroupId());
        }
        //验证需添加的人员类别名是否已存在
        OmcUserType omcUserType = new OmcUserType();
        omcUserType.setUserTypeName(omcUserTypeDto.getUserTypeName());
        omcUserType.setGroupId(omcUserTypeDto.getGroupId());
        int m = omcUserTypeService.selectCount(omcUserType);
        if (m > 0) {//需添加的人员类别已存在,不能重复添加.
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("user.type.exist.error", null), m);
        }
        int n = saveOmcUserType(omcUserTypeDto);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.insert.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.insert.success", null), n);
    }

    /**
     * 修改人员类别
     *
     * @param omcUserTypeDto
     * @param br
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody OmcUserTypeDto omcUserTypeDto, BindingResult br) {
        //验证参数
        if (null == omcUserTypeDto.getId() || omcUserTypeDto.getId() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        String[] validateParam = {"userTypeName"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            /* 返回参数校验错误详情 */
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("user.type.edit.param.error ", null), errorMap);
        }
        if (null == omcUserTypeDto.getGroupId() || omcUserTypeDto.getGroupId() <= 0) {
            //没传机构ID,存储登录用户的机构ID
            omcUserTypeDto.setGroupId(getLoginAuthDto().getGroupId());
        }
        //验证需修改的人员类别名是否已存在
        int m = omcUserTypeService.isNameExist(omcUserTypeDto);
        if (m <= 0) {//需修改的人员类别名不存在可以修改
            int n = saveOmcUserType(omcUserTypeDto);
            if (n <= 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.update.fail", null), n);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.update.success", null), n);
        }
        return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("user.type.exist.error", null), m);
    }


    /**
     * 通过ID查询人员类别信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(Integer id) {
        //验证参数
        if (null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        OmcUserType omcUserType = omcUserTypeService.selectByKey(id);
        if (null == omcUserType) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), omcUserType);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), omcUserType);
    }

    /**
     * 通过ID删除人员类别信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Wrapper deleteById(Long id) {
        //验证参数
        if (null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        int n = omcUserTypeService.deleteOmcUserType(id);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.delete.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.delete.sucess", null), n);
    }

    /**
     * 根据人员类别名模糊分页查询人员类别信息
     *
     * @param omcUserTypeQueryDto
     * @return
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody OmcUserTypeQueryDto omcUserTypeQueryDto) {
        if (null == omcUserTypeQueryDto.getGroupId() || omcUserTypeQueryDto.getGroupId() <= 0) {
            //没传机构ID,存储登录用户的机构ID
            omcUserTypeQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        }
        PageHelper.startPage(omcUserTypeQueryDto.getPageNum(), omcUserTypeQueryDto.getPageSize());
        List<OmcUserType> list = omcUserTypeService.selectByName(omcUserTypeQueryDto);
        PageInfo<OmcUserType> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), pageInfo);
//        return WrapMapper.ok(list);
    }


    /**
     *
     * @param: [uacPlanDto]
     * @return: java.lang.Integer
     */
    private Integer saveOmcUserType(OmcUserTypeDto omcUserTypeDto) {
        OmcUserType omcUserType = new OmcUserType();
        BeanUtils.copyProperties(omcUserTypeDto, omcUserType);
        return omcUserTypeService.saveOmcUserType(omcUserType, getLoginAuthDto());
    }


}
